﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Text.RegularExpressions;
using System.Xml;
using System.Data;
using StockStar.GatherData;

namespace Report
{
    public class BlockTrade
    {
        public string StrConnection = SQLHelper.StrConn;
        public string SZReg = "<tr  class='cls-data-tr' bgcolor='((#FFFFFF)|(WhiteSmoke))' height='30px'>(<td[^>]*>(.{0,100})<\\/td>){8}</tr>";
        public string HSReg = "<tr bgcolor=\"#FFFFFF\">(\\s*<td[^>]*>\\s*(.*)\\s*<\\/td>\\s*){8}</tr>";
        public string SZUrl = "http://www.szse.cn/main/disclosure/news/dzjy/";
        public string HSUrl = "http://www.sse.com.cn/sseportal/webapp/datapresent/SSELargeTradeInfoAct?CURSOR=1";
        public void Import()
        {
            WebClient client = new WebClient();
            string str = client.DownloadString(HSUrl);
            var mc = Regex.Matches(str, HSReg);
            for (int i = 0; i < mc.Count; i++)
            {
                BlockTradeEntity c = new BlockTradeEntity();
                c.SC = "沪市";
                string aa = mc[i].Value.Replace("&", "&amp;");
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(aa);
                var root = doc.DocumentElement;

                try
                {
                    c.JYRQ = DateTime.Parse(root.ChildNodes[0].InnerText);
                }
                catch
                {
                }
                string str2 = root.ChildNodes[1].InnerText;
                if (!string.IsNullOrEmpty(str2) && str2.IndexOf("(") > -1)
                {
                    c.StockName = str2.Substring(0, str2.IndexOf("("));
                    var tt = Regex.Match(str2, "\\d{6}");

                    c.StockId = tt.Value;
                }
                try
                {
                    c.CJJG = decimal.Parse(root.ChildNodes[2].InnerText);
                }
                catch
                {
                }
                try
                {
                    c.CJJE = decimal.Parse(root.ChildNodes[3].InnerText);
                }
                catch
                {
                }
                try
                {
                    c.CJL = decimal.Parse(root.ChildNodes[4].InnerText);
                }
                catch
                {
                }
                c.BuyYYB = root.ChildNodes[5].InnerText;
                c.SellYYB = root.ChildNodes[6].InnerText;
                Insert(c);
            }
            str = client.DownloadString(SZUrl);
            mc = Regex.Matches(str, SZReg);
            for (int i = 0; i < mc.Count; i++)
            {
                BlockTradeEntity c = new BlockTradeEntity();
                c.SC = "深市";
                string aa = mc[i].Value.Replace("&", "&amp;");
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(aa);
                var root = doc.DocumentElement;

                try
                {
                    c.JYRQ = DateTime.Parse(root.ChildNodes[0].InnerText);
                }
                catch
                {
                }
                c.StockId = root.ChildNodes[1].InnerText;
                c.StockName = root.ChildNodes[2].InnerText;
                try
                {
                    c.CJJG = decimal.Parse(root.ChildNodes[3].InnerText);
                }
                catch
                { }
                try
                {
                    c.CJL = decimal.Parse(root.ChildNodes[4].InnerText);
                }
                catch
                {
                }
                try
                {
                    c.CJJE = decimal.Parse(root.ChildNodes[5].InnerText);
                }
                catch
                {
                }
                c.BuyYYB = root.ChildNodes[6].InnerText;
                c.SellYYB = root.ChildNodes[7].InnerText;

                Insert(c);
            }
        }

        public void Insert(BlockTradeEntity c)
        {
            string sql1 = "select * from dbo.BlockTrade where stockid=@stockid and JYRQ=@JYRQ and CJJG=@CJJG and CJJE=@CJJE and CJL=@CJL and BuyYYB=@BuyYYB and SellYYB=@SellYYB and SC=@SC";
            Tools tool = new Tools();
            tool.AddSqlParameter("stockid", System.Data.SqlDbType.VarChar, c.StockId);
            tool.AddSqlParameter("JYRQ", System.Data.SqlDbType.DateTime, c.JYRQ);
            tool.AddSqlParameter("CJJG", System.Data.SqlDbType.Decimal, c.CJJG);
            tool.AddSqlParameter("CJJE", System.Data.SqlDbType.Decimal, c.CJJE);
            tool.AddSqlParameter("CJL", System.Data.SqlDbType.Decimal, c.CJL);
            tool.AddSqlParameter("BuyYYB", System.Data.SqlDbType.VarChar, c.BuyYYB);
            tool.AddSqlParameter("SellYYB", System.Data.SqlDbType.VarChar, c.SellYYB);
            tool.AddSqlParameter("SC", System.Data.SqlDbType.VarChar, c.SC);
            DataSet ds = SQLHelper.ExecuteSet(StrConnection, CommandType.Text, sql1, tool.GetSqlParameters());
            if (ds.Tables.Count > 0)
            {
                if (ds.Tables[0].Rows.Count > 0)
                {
                    return;
                }
            }
            string sql2 = "insert into BlockTrade values(@stockid,@stockname,@jyrq,@cjjg,@cjje,@cjl,@BuyYYB,@SellYYB,@sc)";
            tool = new Tools();
            tool.AddSqlParameter("stockid", SqlDbType.VarChar, c.StockId);
            tool.AddSqlParameter("stockname", SqlDbType.VarChar, c.StockName);
            tool.AddSqlParameter("jyrq", SqlDbType.DateTime, c.JYRQ);
            tool.AddSqlParameter("cjjg", SqlDbType.Decimal, c.CJJG);
            tool.AddSqlParameter("cjje", SqlDbType.Decimal, c.CJJE);
            tool.AddSqlParameter("cjl", SqlDbType.Decimal, c.CJL);
            tool.AddSqlParameter("BuyYYB", SqlDbType.VarChar, c.BuyYYB);
            tool.AddSqlParameter("SellYYB", SqlDbType.VarChar, c.SellYYB);
            tool.AddSqlParameter("sc", SqlDbType.VarChar, c.SC);
            SQLHelper.ExecuteNonQuery(StrConnection, CommandType.Text, sql2, tool.GetSqlParameters());
        }
    }

    public class BlockTradeEntity
    {
        /// <summary>
        /// 股票代码
        /// </summary>
        public string StockId
        {
            get;
            set;
        }

        /// <summary>
        /// 股票名称
        /// </summary>
        public string StockName
        {
            get;
            set;
        }

        /// <summary>
        /// 成交量
        /// </summary>
        public decimal? CJL
        {
            get;
            set;
        }

        /// <summary>
        /// 交易日期
        /// </summary>
        public DateTime? JYRQ
        {
            get;
            set;
        }

        /// <summary>
        /// 成交价格
        /// </summary>
        public decimal? CJJG
        {
            get;
            set;
        }

        /// <summary>
        /// 成交金额
        /// </summary>
        public decimal? CJJE
        {
            get;
            set;
        }

        /// <summary>
        /// 买方营业部
        /// </summary>
        public string BuyYYB
        {
            get;
            set;
        }

        /// <summary>
        /// 卖方营业部
        /// </summary>
        public string SellYYB
        {
            get;
            set;
        }

        /// <summary>
        /// 市场
        /// </summary>
        public string SC
        {
            get;
            set;
        }
    }
}
